package lk.mrt.cse.wihidum.router.processor;

import org.apache.log4j.Logger;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.Options;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.AxisFault;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import lk.mrt.cse.wihidum.util.WihidumConstants;
import lk.mrt.cse.wihidum.util.WSEventingConstants;
import lk.mrt.cse.wihidum.client.BrokerList;

import javax.xml.namespace.QName;
import java.util.Iterator;
import java.util.ArrayList;

public class SubscriptionProcessor {

    private static final Logger logger = Logger.getLogger (SubscriptionProcessor.class);

    public String subscribe(String subscriptionID, String brokerAddress, String adminAddress, String listenerAddress, String subscriberAddress, String topic) {
        try
        {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(subscriberAddress));
            options.setAction("subscribe");
            serviceClient.setOptions(options);

            OMFactory factory = OMAbstractFactory.getOMFactory();

            OMElement messageElement = factory.createOMElement("SubscriptionRequest", null);

            OMElement subscriptionHeader = factory.createOMElement("Header", null);
            OMElement actionElement = factory.createOMElement("Action", null);
            actionElement.setText("subscribe");
            subscriptionHeader.addChild(actionElement);
            messageElement.addChild(subscriptionHeader);

            OMElement subscriptionIDElement = factory.createOMElement("subscriptionID", null);
            subscriptionIDElement.setText(subscriptionID);
            messageElement.addChild(subscriptionIDElement);

            OMElement brokerAddressElement = factory.createOMElement("brokerAddress", null);
            brokerAddressElement.setText(brokerAddress);
            messageElement.addChild(brokerAddressElement);

            OMElement publisherAddressElement = factory.createOMElement("adminAddress", null);
            publisherAddressElement.setText(adminAddress);
            messageElement.addChild(publisherAddressElement);

            OMElement listenerAddressElement = factory.createOMElement("listenerAddress", null);
            listenerAddressElement.setText(listenerAddress);
            messageElement.addChild(listenerAddressElement);

            OMElement subscriberAddressElement = factory.createOMElement("subscriberAddress", null);
            subscriberAddressElement.setText(subscriberAddress);
            messageElement.addChild(subscriberAddressElement);

            OMElement topicElement = factory.createOMElement("topic", null);
            topicElement.setText(topic);
            messageElement.addChild(topicElement);

            OMElement responseElement = serviceClient.sendReceive(messageElement);
            return responseElement.getText();

        }
        catch (NullPointerException ex)
        {
            return "Error in processing subscription response";
        }
        catch (AxisFault ex)
        {
             return "Error in sending subscription request";
        }
    }
    public ArrayList<String> getsubscribedTopics(String listenerAddress, String subscriptionID)
    {
        ArrayList<String> subscribedTopics= new ArrayList<String>();
        OMFactory factory = OMAbstractFactory.getOMFactory();

        OMElement messageElement = factory.createOMElement("GetSubscribedTopicsRequest", null);

        OMElement actionElement = factory.createOMElement("Action", null);
        actionElement.setText(WihidumConstants.Operations.GET_SUBSCRIBED_TOPICS);
        messageElement.addChild(actionElement);

        OMElement subscriptionIDElement = factory.createOMElement("subscriptionID", null);
        subscriptionIDElement.setText(subscriptionID);
        messageElement.addChild(subscriptionIDElement);

        OMElement responseElement=null;

        try
        {
            ServiceClient serviceClient = new ServiceClient();
            Options options = new Options();
            options.setTo(new EndpointReference(listenerAddress));
            options.setAction(WihidumConstants.Operations.GET_SUBSCRIBED_TOPICS);
            serviceClient.setOptions(options);
            responseElement = serviceClient.sendReceive(messageElement);


        }
        catch (Exception ex)
        {
            return null;
        }
        String responseInfo=responseElement.getText();
        if(responseInfo.equals("TopicsExist"))
        {
            logger.info("Router service received a non null response");

             OMElement topicListElement=responseElement.getFirstChildWithName(new QName("TopicsList"));
             System.out.println(topicListElement);
                        
             for(Iterator<OMElement> topicElementIterator= (Iterator<OMElement>)topicListElement.getChildrenWithName(new QName("Topic")); topicElementIterator.hasNext();)
             {
                   OMElement topicElement=topicElementIterator.next();
                   String topic=topicElement.getText();
                   subscribedTopics.add(topic);
                   logger.info("Topic no: "+topic+" is added to topics array.");
             }
        }
        else if(responseInfo.equals("TopicsNotExist"))
        {
                logger.info("Received response at router service is null");
        }
        return subscribedTopics;
    }
}
